package saas.dev.cloud.util.test;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName : SortUtil
 * @Author : y1
 * @Date: 2022/5/20 17:15
 * @Description :
 */
public class SortUtil {


    public static void main(String[] args) {
        List<SortBean> list = new ArrayList<>();
        SortBean sortBean = new SortBean();
        sortBean.setId("A");
        sortBean.setSortNum(0);
        list.add(sortBean);
        sortBean = new SortBean();
        sortBean.setId("B");
        sortBean.setSortNum(1);
        list.add(sortBean);
        sortBean = new SortBean();
        sortBean.setId("C");
        sortBean.setSortNum(2);
        list.add(sortBean);
        sortBean = new SortBean();
        sortBean.setId("D");
        sortBean.setSortNum(3);
        list.add(sortBean);
        sortBean = new SortBean();
        sortBean.setId("E");
        sortBean.setSortNum(4);
        list.add(sortBean);
        System.out.println("原数据list：" + JSON.toJSONString(list));

        SortBean metaData = new SortBean();
        metaData.setId("B");
        metaData.setSortNum(1);
        System.out.println("要修改的数据的原始数据：" + JSON.toJSONString(metaData));

        sortBean = new SortBean();
        sortBean.setId("B");
        sortBean.setSortNum(3);
        System.out.println("要修改的数据：" + JSON.toJSONString(sortBean));

        System.out.println(extracted(list, sortBean, metaData));

        /*
        Map<Integer, TwMainBusinessType> collect1 = list.stream().collect(Collectors.toMap(TwMainBusinessType::getSortNum, i -> i));
        TreeMap<Integer, TwMainBusinessType> collect = new TreeMap<Integer, TwMainBusinessType>(collect1);
        boolean b = collect.containsKey(sortBean.getSortNum());
        System.out.println(collect.keySet().size());
        SortedMap<Integer, TwMainBusinessType> integerTwMainBusinessTypeSortedMap = collect.subMap(5, collect.keySet().size());
        for (TwMainBusinessType value : integerTwMainBusinessTypeSortedMap.values()) {
            value.setSortNum(value.getSortNum() + CommonConstants.ONE);
        }
        System.out.println(JSON.toJSONString(integerTwMainBusinessTypeSortedMap));
        System.out.println(JSON.toJSONString(collect));*/
    }

    /**
     * @param list     原始数据
     * @param sortBean 修改的数据
     * @param metaData 修改的数据的原始数据
     */
    private static List<SortBean> extracted(List<SortBean> list, SortBean sortBean, SortBean metaData) {
        for (int i = 0; i < list.size(); i++) {
            var item = list.get(i);
            if (item.getId().equals(sortBean.getId()) && i == list.size() - 2) {
                return Collections.emptyList();
            }
            if (item.getId().equals(sortBean.getId()) && i == list.size() - 1) {
                return Collections.emptyList();
            }
        }
        List<SortBean> oldList = list.stream().map(i -> {
            SortBean item = new SortBean();
            BeanUtils.copyProperties(i, item);
            return item;
        }).collect(Collectors.toList());
        SortBean temp = new SortBean();
        for (SortBean mainBusinessType : list) {
            if (mainBusinessType.getId().equals(sortBean.getId())) {
                BeanUtils.copyProperties(temp, mainBusinessType);
                continue;
            }
            if (mainBusinessType.getSortNum().equals(sortBean.getSortNum())) {
                BeanUtils.copyProperties(mainBusinessType, temp);
                BeanUtils.copyProperties(sortBean, mainBusinessType);
            }
        }
        System.out.println("要替换位置数据：" + JSON.toJSONString(temp));

        Map<Integer, SortBean> collect1 = oldList.stream().collect(Collectors.toMap(SortBean::getSortNum, i -> i));
        TreeMap<Integer, SortBean> collect = new TreeMap<Integer, SortBean>(collect1);
        int start, end;
        if (metaData.getSortNum() > temp.getSortNum()) {
            start = temp.getSortNum();
            end = metaData.getSortNum();
        } else {
            start = metaData.getSortNum();
            end = temp.getSortNum();
        }
        SortedMap<Integer, SortBean> integerTwMainBusinessTypeSortedMap = collect.subMap(start, end);
        for (SortBean value : integerTwMainBusinessTypeSortedMap.values()) {
            value.setSortNum(value.getSortNum() + 1);
        }
        return new ArrayList<>(integerTwMainBusinessTypeSortedMap.values());
        /*Map<String, SortBean> ids = integerTwMainBusinessTypeSortedMap.values().stream().collect(Collectors.toMap(SortBean::getId, i -> i));

        for (SortBean mainBusinessType : list) {
            SortBean i = ids.get(mainBusinessType.getId());
            if (Validator.isNotNull(i)) {
                mainBusinessType.setId(i.getId());
                mainBusinessType.setSortNum(i.getSortNum());
            }
        }
        System.out.println("更换后数据：" + JSON.toJSONString(list));
        System.out.println("排序后数据：" + JSON.toJSONString(list.stream().sorted(Comparator.comparing(SortBean::getSortNum)).collect(Collectors.toList())));*/
    }
}
